package nfa;

import interfaces.Node;
import interfaces.Transition;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Ricardo Sansores
 */
public class AutomataNode implements Node {

    private List<Transition> transitions;
    private boolean aceptanceNode = false;
    private String name;

    public AutomataNode(String label) {
        this.transitions = new ArrayList<Transition>();
        this.name = label;
    }

    public AutomataNode(List<Transition> transitions) {
        this.transitions = transitions;
    }

    public AutomataNode(List<Transition> transitions, boolean aceptanceNode) {
        this.transitions = transitions;
        this.aceptanceNode = aceptanceNode;
    }

    public void addTransition(Transition transition) {
        transitions.add(transition);
    }

    /**
     * @return the transition
     */
    public List<? extends Transition> getTransitions() {
        return transitions;
    }

    /**
     * @param transition the transition to set
     */
    public void setTransitions(List<Transition> transitions) {
        this.transitions = transitions;
    }

    /**
     * @return the aceptanceNode
     */
    public boolean isAceptanceNode() {
        return aceptanceNode;
    }

    /**
     * @param aceptanceNode the aceptanceNode to set
     */
    public void setAceptanceNode(boolean aceptanceNode) {
        this.aceptanceNode = aceptanceNode;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * Devuelve la primer transición que se encuentre en la lista de transiciones del nodo
     * cuyo nodo de salida tenga el nombre proporcionado.
     * @return La primer ocurrencia o null si no existe ninguna transición cuyo nodo de salida
     * tenga el nombre proporcionado
     */
    public Transition getTransitionByOutputNode(String outputNodeName) {
        for (Transition transition : transitions) {
            if (transition.getToNodeName().equals(outputNodeName)) {
                return transition;
            }
        }
        return null;
    }

    /**
     * Limpia la lista de transiciones del nodo.
     */
    public void clearTransitions() {
        setTransitions(new ArrayList<Transition>());
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final AutomataNode other = (AutomataNode) obj;
        if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 37 * hash + (this.name != null ? this.name.hashCode() : 0);
        return hash;
    }
    
    
}
